package com.faxsun.profile.core.dao;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.broadleafcommerce.common.persistence.EntityConfiguration;
import org.broadleafcommerce.profile.core.domain.Customer;
import org.hibernate.ejb.QueryHints;
import org.springframework.stereotype.Repository;

import com.faxsun.profile.core.domain.FSAssistant;


@Repository("blAssistantDao")
public class FSAssistantDaoImpl implements FSAssistantDao {
	@PersistenceContext(unitName = "blPU")
	protected EntityManager em;

	@Resource(name = "blEntityConfiguration")
	protected EntityConfiguration entityConfiguration;
	
	
	@Override
	public FSAssistant create(String md5) {
		FSAssistant assistant = (FSAssistant) entityConfiguration.createEntityInstance(FSAssistant.class.getName());
		assistant.setMD5(md5);
		return assistant;
	}

	@Override
	public FSAssistant save(FSAssistant assistant) {
		return em.merge(assistant);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Customer> readCustomerByMD5(String md5) {
		Query query = em
				.createQuery("SELECT customer FROM com.faxsun.profile.core.domain.FSAssistant assistant"
						+ " where assistant.md5Id=:md5Id");
        query.setParameter("md5Id", md5);
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        query.setHint(QueryHints.HINT_CACHE_REGION, "query.FSThirdPartyCustomer");
        return query.getResultList();  
	}

}
